<template>
    <div class="goods">
        <div class="content">
            <!-- 面包屑 -->
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item>商品列表</el-breadcrumb-item>
            </el-breadcrumb>
            <!-- 搜索框 -->
            <div class="list_input">
                <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
                    <el-button slot="append" icon="el-icon-search"></el-button>
                </el-input>
                <el-button type="success" plain class="add" @click="add">添加商品</el-button>
            </div>
            <!-- 表格 -->
            <div class="table">
                <el-table :data="tableData" height="100%" border style="width: 100%" v-show="show" ref="singleTable" stripe>
                    <el-table-column type="index" width="50" height="100"></el-table-column>
                    <el-table-column prop="goods_name" label="商品名称" width="780"></el-table-column>
                    <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
                    <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
                    <el-table-column prop="upd_time" label="创建日期" width="100">
                      <template slot-scope="scope">
                          <div>{{scope.row.upd_time | time1}}</div>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作">
                      <template slot-scope="scope">
                        <el-button  type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.$index, scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="handleDelete(scope.$index, scope.row)"></el-button>
                      </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- 分页 -->
            <div class="block">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage4"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { goods,dels } from "../api/api.js";
export default {
    data() {
      return {
        input: "",
        tableData: [],
        total:0,
        pagesize:10,
        currentRow: null,
        currentPage4: 4,
        show:true
      }
    },
    filters:{
        time1:function(val){
            var now = new Date(val);
            var year = now.getFullYear();
            var month = now.getMonth() + 1;
            var data = now.getDate();
            return (
                year +
                "-" +
                (month < 10 ? "0" + month : month) +
                "-" +
                (data < 10 ? "0" + data : data)
            );
         }
    },
    methods:{
        setCurrent(row) {
            this.$refs.singleTable.setCurrentRow(row);
        },
         handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pagesize = val;
            this.getgoods();
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.currentPage4 = val;
            this.getgoods();
        },
        getgoods(){
            goods({
                query: "",
                pagenum: 1,
                pagesize: this.pagesize
            }).then(res=>{
                if(res.data.meta.status==200){
                    console.log("商品详情数据",res);
                    this.tableData=res.data.data.goods;
                    this.total=res.data.data.total
                }
            })
        },
        // 添加商品
        add(){
            this.show = false;
            this.$router.push({
                name:"Addgoods"
            })
        },
        // 编辑
        handleEdit(scope,row){
            console.log(scope);
            console.log(row);
        },
        // 删除
        handleDelete(scope,row){
            console.log(row.goods_id);
            dels({
            id:row.goods_id
            }).then(res=>{
            console.log("删除成功",res)
            return res
            }).then(res=>{
            this.getgoods()
            })
        }
    },
    created(){
        this.getgoods();
    }
    
  }
</script>

<style scoped lang="scss">
    .goods{
        width:85%;
        height:93%;
        background-color:white;
        margin:22px auto;
        display:flex;
        justify-content: center;
        align-items:center;
        .content{
            width:97%;
            height:95%;
            .list_input{
                margin-top:12px;
                .el-input{
                    width:360px;
                }
            }
            .table{
                margin-top:10px;
                height:73%;
            }
            .block{
                margin-top:10px;
            }
        }
    }
</style>
